﻿using log4net;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Windows.Controls;
using TruckScale.Models;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for DMCustomerDetailView.xaml
  /// </summary>
  public partial class DMCustomerDetailView : RadWindow, INotifyPropertyChanged
  {
    //log
    private static readonly log4net.ILog log = LogManager.GetLogger("DMCustomerDetailView");
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;

    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];
    public CustomerModel Customer { get; set; }
    //public string CustomerLevel { get; set; } = "一级";
    public DMCustomerDetailView(CustomerModel customer)
    {
      InitializeComponent();
      this.DataContext = this;

      if (customer != null)
      {
        Customer = customer;
        //CustomerLevel = customer.CustomerLevel;
        if (Customer.CustomerType != null)
        {
          if (Customer.CustomerType.Contains("采购")) cbType1.IsChecked = true;
          if (Customer.CustomerType.Contains("销售")) cbType2.IsChecked = true;
          if (Customer.CustomerType.Contains("运输")) cbType3.IsChecked = true;
          if (Customer.CustomerType.Contains("其他")) cbType4.IsChecked = true;
        }
      }
      else
      {
        Customer = new CustomerModel();
      }
    }

    private void RadButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      //Customer.CustomerLevel = CustomerLevel;

      if (Customer.CustomerId == null)
      {
        if (0 < CustomerModel.InsertCustomer(Customer))
        {
          this.Close();
        }
        else
        {
          InfoBar = "保存失败，请检查信息是否完整或已存在！";
        }
      }
      else
      {
        if (0 < CustomerModel.UpdateCustomer(Customer))
        {
          this.Close();
        }
        else
        {
          InfoBar = "保存失败，请检查信息是否完整或已存在！";
        }
      }
    }

    private void CheckBox_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      List<string> types = new List<string>();
      if (cbType1.IsChecked == true) types.Add("采购");
      if (cbType2.IsChecked == true) types.Add("销售");
      if (cbType3.IsChecked == true) types.Add("运输");
      if (cbType4.IsChecked == true) types.Add("其他");
      Customer.CustomerType = string.Join(",", types);
    }
  }
}
